﻿using System;

public class Node<T>
{
    public T Data { get; set; }
    public Node<T>? Next { get; set; }

    public Node(T t)
    {
        Data = t;
        Next = null;
    }
}

public class GenericLinkedList<T>
{
    private Node<T>? head;
    private Node<T>? tail;

    public GenericLinkedList()
    {
        tail = head = null;
    }
    public Node<T>? Head
    {
        get => head;
    }

    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }

    public void ForEach(Action<T> action)
    {
        Node<T> current = head;
        while (current != null)
        {
            action(current.Data);
            current = current.Next;
        }
    }

    public int Count
    {
        get
        {
            int count = 0;
            Node<T> current = head;
            while (current != null)
            {
                count++;
                current = current.Next;
            }
            return count;
        }
    }
}
class Program
{
    static void Main()
    {
        var list = new GenericLinkedList<int>();
 
        list.Add(10);
        list.Add(20);
        list.Add(5);
        list.Add(15);
        list.Add(30);

        Console.WriteLine("链表元素:");
        list.ForEach(x => Console.WriteLine(x));

        int max = int.MinValue; 
        int min = int.MaxValue;  
        int sum = 0;             

        list.ForEach(x =>
        {
            if (x > max) max = x;   
            if (x < min) min = x;   
            sum += x;                
        });

        Console.WriteLine($"最大值: {max}");
        Console.WriteLine($"最小值: {min}");
        Console.WriteLine($"总和: {sum}");
    }
}
